<?
class sym_category_admin extends symbiont{
    public function ___main($design, $for='', $id=0){
        global $kernel, $sql;
        $this->for_symbiont=$for;
        $design=$this->_check($design, 'admin', 'category');
        $d=new design($design, 'category_admin', false);
        if(!data::is_int($id)) $id=$sql->select_each('sym_category', 'id', array('name'=>data::word($id)), '', '', 1);
        else $id=data::number($id);
        $this->content=$this->categories($id, $for);
        $d->inc();
    }
    public function ___add($design='', $for=''){
        global $sql;
        $design=$this->_check($design, 'add', 'category');
        $d=new design($design, 'category_admin', false);
        
        $this->parent_id=0;
        $this->category_name='';
        $this->title='';
        $this->content='';
        $this->cover='';
        $this->current_id=0;
        
        $this->what='add';
        $this->infos='';
        $this->languages='';
        $languages=$sql->select_each('languages', 'abbr', array('is_enabled'=>1), '', 'position');
        foreach($languages as $val){
            $this->abbr=$val['abbr'];
            $this->languages.=design::run($this->language);
            $this->infos.=design::run($this->info);
        }
        
        $this->categories=$this->categories(0, $for);
        
        $d->inc();
    }
    public function ___edit($design='', $for, $id){
        global $sql;
        $design=$this->_check($design, 'add', 'category');
        $d=new design($design, 'category_admin', false);
        
        $id=data::number($id);
        
        $cats=$sql->query_each("SELECT c.id, c.parent_id, c.name, c.cover, p.title, p.content, l.abbr  FROM `sym_category` as c LEFT JOIN `paragraphs` as p ON c.paragraph_id=p.id LEFT JOIN `languages` as l ON p.lng_id=l.id WHERE c.id=".$id."");
        
        $title=array();
        $content=array();
        if(is_array($cats)){
            foreach($cats as $val){
                $title[$val['abbr']]=$val['title'];
                $content[$val['abbr']]=$val['content'];
            }
            $this->category_name=$cats[0]['name'];
            $this->cover=$cats[0]['cover'];
            $this->parent_id=$cats[0]['parent_id'];
            $this->current_id=$cats[0]['id'];
        }
        else{
            $this->category_name='';
            $this->cover='';
            $this->parent_id='';
            $this->current_id=0;
        }
        
        $this->what='edit';
        $this->infos='';
        $this->languages='';
        $languages=$sql->select_each('languages', 'abbr', array('is_enabled'=>1), '', 'position');
        foreach($languages as $val){
            $this->title=isset($title[$val['abbr']])?$title[$val['abbr']]:'';
            $this->content=isset($content[$val['abbr']])?$content[$val['abbr']]:'';
            $this->abbr=$val['abbr'];
            $this->languages.=design::run($this->language);
            $this->infos.=design::run($this->info);
        }
        $this->categories=$this->categories(0, $for);
        $d->inc();
    }
    public function ___delete($design=''){
        $design=$this->_check($design, 'delete', 'category');
        $d=new design($design, 'category_admin');
    }
    
    //Db functions
    public function ___db_add($design=''){
        global $sql, $user, $kernel;
        if($user->access_level<9){
            print('{"error":"You have not enought prerogatives."}');
            return;
        }
        if(!isset($_POST['name'])||!isset($_POST['title'])||!isset($_POST['parent'])||!isset($_POST['title'])||!isset($_POST['cover'])||!isset($_POST['for'])){
            print('{"error":"Not the all parametrs have been sent."}');
            return;
        }
        $attention='';
        $kernel->library_add('paragraph');
        $name=data::word($_POST['name']);
        $for=data::word($_POST['for']);
        $p=new paragraph();
        foreach($_POST['title'] as $key=>$val){
            $title=data::html_remove($val);
            if(!$title) continue;
            $description=data::html_remove($_POST['description'][$key]);
            $abbr=data::word($key);
            $lng_id=$sql->select_each('languages', 'id', array('abbr'=>$abbr), '', '', 1);
            $p->add($lng_id, $title, $description);
        }
        $cover=data::file_system($_POST['cover']);
        $parent=data::html_remove($_POST['parent']);
        if($cover){
            $kernel->library_add('image');
            $image=new image($cover);
            $c=$image->resize_square('', 100, true);
            if($c){
                $cover=$c;
            }
            else{
                $attention.="Can not resize file.";
            }
        }
        $id=$sql->insert('sym_category', array('name'=>$name, 'paragraph_id'=>$p->id, 'cover'=>$cover, 'parent_id'=>$parent, 'for'=>$for));
        $sql->update('sym_category', array('position'=>$id), array('id'=>$id), 1);
        if($id){
            print('{"ok": "Category added.", "attention": "'.$attention.'"}');
        }
        else{
            print('{"error": "Category adding error."}');
        }
    }
    public function ___db_edit($design=''){
        global $sql, $user, $kernel;
        if($user->access_level<9){
            print('{"error":"You have not enought prerogatives."}');
            return;
        }
        if(!isset($_POST['name'])||!isset($_POST['title'])||!isset($_POST['parent'])||!isset($_POST['title'])||!isset($_POST['cover'])||!isset($_POST['id'])||!isset($_POST['for'])){
            print('{"error":"Not the all parametrs have been sent."}');
            return;
        }
        $attention='';
        $kernel->library_add('paragraph');
        $id=data::number($_POST['id']);
        $old=$sql->query_each("SELECT c.cover, p.id as id  FROM `sym_category` as c LEFT JOIN `paragraphs` as p ON c.paragraph_id=p.id WHERE c.id=".$id." LIMIT 1", true);
        $name=data::word($_POST['name']);
        $for=data::word($_POST['for']);
        if(isset($old['id'])&&$old['id']) $p=new paragraph($old['id']);
        else $p=new paragraph();
        foreach($_POST['title'] as $key=>$val){
            $title=data::html_remove($val);
            if(!$title) continue;
            $description=data::html_remove($_POST['description'][$key]);
            $abbr=data::word($key);
            $lng_id=$sql->select_each('languages', 'id', array('abbr'=>$abbr), '', '', 1);
            $p->set($lng_id, $title, $description);
        }
        $cover=data::file_system($_POST['cover']);
        $parent=data::html_remove($_POST['parent']);
        $values=array('name'=>$name, 'paragraph_id'=>$p->id, 'parent_id'=>$parent, 'for'=>$for);
        if($cover){
            if(isset($old['cover'])&&$old['cover']){
                unlink($old['cover']);
            }
            $kernel->library_add('image');
            $image=new image($cover);
            $c=$image->resize_square('', 100, true);
            if($c){
                $cover=$c;
            }
            else{
                $attention.="Can not resize file.";
            }
            $values['cover']=$cover;
        }
        $id=$sql->update('sym_category', $values, array('id'=>$id));
        if($id){
            print('{"ok": "Category updated.", "attention": "'.$attention.'"}');
        }
        else{
            print('{"error": "Category updating error."}');
        }
    }
    public function ___db_delete($design=''){
        global $user;
        if($user->access_level<9||!isset($_POST['id'])) return;
        $id=data::number($_POST['id']);
        $this->delete($id);
    }
    public function ___db_sort($design=''){
        global $user, $sql;
        if($user->access_level<9) return;
        foreach($_POST['sort'] as $key=>$val){
            $id=data::number($val);
            $position=data::number($key)+1;
            $sql->update('sym_category', array('position'=>$position), array('id'=>$id), 1);
        }
    }
    
    //Private functions
    private function delete($id){
        global $sql;
        $categories=$sql->select_each('sym_category', 'id', array('parent_id'=>$id));
        if($categories){
            foreach($categories as $val){
                $this->delete($val['id']);
            }
        }
        $cover=$sql->select_each('sym_category', 'cover', array('id'=>$id), '', '', 1);
        if(file_exists($cover)){
            unlink($cover);
        }
        $sql->delete('sym_category', array('id'=>$id));
    }
    private function categories($id=0, $for='', $prefix='', $r=0, $g=0, $b=0){
        global $sql, $kernel;
        if($r<=0) $r=rand(200, 255);
        if($g<=0) $g=rand(200, 255);
        if($b<=0) $b=rand(200, 255);
        $r='';
        $categories=$sql->query_each("SELECT c.id, c.for, c.name, p.title  FROM `sym_category` as c LEFT JOIN `paragraphs` as p ON c.paragraph_id=p.id AND p.lng_id = ".$kernel->lang->id." WHERE c.parent_id=".$id." ".($for?"AND c.for='".$for."'":'')." ORDER BY `position`");
        if($categories){
            foreach($categories as $val){
                $this->child=$this->categories($val['id'], $for, $prefix.'&nbsp;', $r-10, $g-10, $b-10);
                $this->id=$val['id'];
                $this->name=$val['name'];
                $this->title=$val['title']?$val['title']:$val['name'];
                $this->for_symbiont=$val['for'];
                $this->prefix=$prefix;
                $this->background='#'.$this->hex($r).$this->hex($g).$this->hex($b);
                $r.=design::run($this->category);
            }
        }
        return $r;
    }
    public function pub_categories($id=0, $for='', $prefix='', $r=0, $g=0, $b=0){
        global $sql, $kernel, $sym;
        if($r<=0) $r=rand(200, 255);
        if($g<=0) $g=rand(200, 255);
        if($b<=0) $b=rand(200, 255);
        $r='';
        $categories=$sql->query_each("SELECT c.id, c.for, c.name, p.title  FROM `sym_category` as c LEFT JOIN `paragraphs` as p ON c.paragraph_id=p.id WHERE c.parent_id=".$id." AND (p.lng_id = ".$kernel->lang->id." OR p.title IS NULL) ".($for?"AND c.for='".$for."'":'')." ORDER BY `position`");
        if($categories){
            foreach($categories as $val){
                $sym->$for->child=$this->pub_categories($val['id'], $for, $prefix.'&nbsp;', $r-10, $g-10, $b-10);
                $sym->$for->id=$val['id'];
                $sym->$for->name=$val['name'];
                $sym->$for->title=$val['title']?$val['title']:$val['name'];
                $sym->$for->for_symbiont=$val['for'];
                $sym->$for->prefix=$prefix;
                $sym->$for->background='#'.$this->hex($r).$this->hex($g).$this->hex($b);
                $r.=design::run($sym->$for->category);
            }
        }
        return $r;
    }
    private function hex($dec){
        $hex=dechex($dec);
        if(strlen($hex)==0) return '00';
        if(strlen($hex)==1) return '0'.$hex;
        return $hex;
    }
}
?>